package datastruct.queue;

import datastruct.lineList.LinkList;

/**
 * @author RunningShrimp
 * @date 2021/5/9  20:11
 */
public class LinkQueue<E> implements Queue<E> {

    private Node head, tail;
    private int size;

    public LinkQueue() {
        head = null;
        tail = null;
        size = 0;
    }

    @Override
    public E dequeue() {
        if (size == 0) {
            throw new IllegalStateException("队列为空");
        }
        Node node = head;
        head = head.next;
        node.next = null;
        if (head == null) {
            tail = null;
        }
        size--;
        return node.date;
    }

    @Override
    public void enqueue(E e) {
        Node node = new Node(e);
        if (tail == null) {
            tail = node;
            head = node;
        } else {
            tail.next = node;
            tail = tail.next;
        }
        size++;
    }

    @Override
    public E getFront() {
        return head.date;
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 节点，双向链表
     */
    private class Node {
        public E date;
        public Node next;

        public Node(E e, Node next) {
            this.date = e;
            this.next = next;
        }

        public Node(E e) {
            this(e, null);
        }

        public Node() {
            this(null);
        }

        @Override
        public String toString() {
            return date.toString();
        }
    }
}
